//深拷贝
export const cloneDeep = function(obj) {
    var newobj = obj instanceof Array ? [] : {}
    for (var k in obj) {
        if (typeof obj[k] === 'object') {
            newobj[k] = JSON.stringify(obj[k])
        } else {
            newobj[k] = obj[k]
        }
    }
    return newobj
}


//转汉字
export const convertToChinaNum = function (num) {    
    var arr1 = new Array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');    
    var arr2 = new Array('', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千','万', '十', '百', '千','亿');//可继续追加更高位转换值    
    if(!num || isNaN(num)){        
        return "零";    
    }    
    var english = num.toString().split("")    
    var result = "";    
    for (var i = 0; i < english.length; i++) {        
        var des_i = english.length - 1 - i;//倒序排列设值        
        result = arr2[i] + result;        
        var arr1_index = english[des_i];        
        result = arr1[arr1_index] + result;    
    }    
    //将【零千、零百】换成【零】 【十零】换成【十】    
    result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十');    
    //合并中间多个零为一个零    
    result = result.replace(/零+/g, '零');    
    //将【零亿】换成【亿】【零万】换成【万】    
    result = result.replace(/零亿/g, '亿').replace(/零万/g, '万');    
    //将【亿万】换成【亿】    
    result = result.replace(/亿万/g, '亿');    
    //移除末尾的零    
    result = result.replace(/零+$/, '')    
    //将【零一十】换成【零十】    
    //result = result.replace(/零一十/g, '零十');
    //貌似正规读法是零一十    
    //将【一十】换成【十】    
    result = result.replace(/^一十/g, '十');    
    return result;
 }


// 升序填充数组
export const fillRange = (start, end) => {
    return Array(end - start + 1).fill().map((item, index) => start + index);
}
// 数组拆分chunk
export const arrayChunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
    arr.slice(i * size, i * size + size)
);
// 时间格式化
export const dateFormat = (date, fmt) => {
    const o = {
        "M+": date.getMonth() + 1, //月份   
        "d+": date.getDate(), //日   
        "h+": date.getHours(), //小时   
        "m+": date.getMinutes(), //分   
        "s+": date.getSeconds(), //秒   
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
        "S": date.getMilliseconds() //毫秒   
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
// 排序
export const sortArray = (arr, property) => {
    return arr.sort((a, b) => {
        return a[property] - b[property]
    })
}
export const timeAgo = (dateTimeStamp) => {
    let result = ''
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - Date.parse(dateTimeStamp);
    if (diffValue < 0) { return; }
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    if (monthC >= 1) {
        result = "" + parseInt(monthC) + "月前";
    } else if (weekC >= 1) {
        result = "" + parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
        result = "" + parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
        result = "" + parseInt(hourC) + "小时前";
    } else if (minC >= 1) {
        result = "" + parseInt(minC) + "分钟前";
    } else
        result = "刚刚";
    return result;
}

export const Addday = (dateTemp,days,isdate)=> {
    dateTemp=+dateTemp +1000*60*60*24*days;
    console.log(days);
    console.log(dateTemp);
    dateTemp=new Date(dateTemp);
    if(isdate){
        return dateTemp.getFullYear()+"/"+(dateTemp.getMonth()+1)+"/"+dateTemp.getDate();
    }
    else{
        return dateTemp.getFullYear()+"/"+(dateTemp.getMonth()+1)+"/"+dateTemp.getDate()+" "+dateTemp.getHours()+":"+dateTemp.getMinutes()+":"+dateTemp.getSeconds();
    }
}


//获取设备像素比
export const getPixelRatio = () => {
    let pixelRatio = 0
 
    var windowswidth = wx.getSystemInfoSync().windowWidth;
     pixelRatio = windowswidth / 750; // wx.getSystemInfoSync().pixelRatio;
    return pixelRatio
  }


  
export const formatdisplaycount = (count)=> {
    
    if(count<1000){
        return count;
    }
    else if(count>=1000 && count<1000000){
        var display =  count/1000;
        var result = display.toString().substring(0,display.toString().indexOf(".")+3);
        return result + ' K';
    }
    else if(count>=1000000 &&  count<1000*1000*1000){
        var display =  count/(1000 * 1000);
        var result = display.toString().substring(0,display.toString().indexOf(".")+3);
        return result + ' M';
    }
    else if(count>=1000*1000*1000 &&  count<1000*1000*1000*1000){
        var display =  count/(1000*1000*1000 );
        var result = display.toString().substring(0,display.toString().indexOf(".")+3);
        return result + ' B';
    }
    else if(count>=1000*1000*1000*1000 &&  count<1000*1000*1000*1000*1000){
        var display =  count/(1000*1000*1000*1000);
        var result = display.toString().substring(0,display.toString().indexOf(".")+3);
        return result + ' G';
    }
}


//获取比赛名称
export const getmatchshortname = function(matchname) {
  var indexfirst = matchname.indexOf('【');
  var indexlast =  matchname.indexOf('】');
     
 var shortmatchname = indexfirst >0 &&  indexlast >0 ?matchname.substring(indexfirst+1,indexlast):matchname;
  return shortmatchname
}